<?php

class Composite_Exception extends CMS_Exception {}
class Composite_Not_Found_Exception extends Composite_Exception {}

class Composite {

	protected $id;
	protected $site;
	protected $name;
	protected $components;

	public static function GetByID($id){
		$result = Database::getInstance()->query("SELECT * from `composites` WHERE `id` = %u AND `site` = %u", $id, Site::getInstance()->GetID());
		if(!($composite = $result->fetch_object('Composite'))){
			throw new Composite_Not_Found_Exception();
		}
		return $composite;
	}

	public function id(){
		return $this->id;
	}

	public function getComponents(){
		if(!isset($this->components)){
			$this->components = Composite_Component::GetByComposite($this->id);
		}
		return $this->components;
	}

}

class Composite_Component {

	protected $id;
	protected $site;
	protected $composite;
	protected $component;
	protected $order;

	protected $page;

	public function component(){
		return $this->component;
	}

	public function order(){
		return $this->order;
	}

	public function id(){
		return $this->id;
	}

	public static function GetByComposite($id){
		$result = Database::getInstance()->query("SELECT * from `composite_components` WHERE `composite` = %u AND `site` = %u", $id, Site::getInstance()->GetID());
		$return = array();
		while($composite = $result->fetch_object('Composite_Component')){
			$return[] = $composite;
		}
		return $return;
	}

}
